import pygame
import random
from pygame.locals import *

# 初始化Pygame
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
font = pygame.font.SysFont('arial', 24)

# 阵营配置（参考‌:ml-citation{ref="1" data="citationList"}的阵营设计）
FACTIONS = {
    "军事要塞": {"defense": 120, "resources": 800, "tech": 30, "prod": 100},
    "科技联盟": {"defense": 80, "resources": 500, "tech": 100, "prod": 80},
    "工业帝国": {"defense": 100, "resources": 1500, "tech": 20, "prod": 140}
}


class Base:
    def __init__(self, faction):
        self.defense = FACTIONS[faction]["defense"]    # 防御值
        self.resources = FACTIONS[faction]["resources"] # 资源量
        self.tech = FACTIONS[faction]["tech"]  # 科技点
        self.production = FACTIONS[faction]["prod"]  # 生产率
        self.wave = 1   # 丧尸波次
        self.faction = faction  # 阵营


# 定义一个绘制按钮的函数，参数为按钮上的文字、按钮的左上角坐标、按钮的宽度和高度
def draw_button(text, x, y, w, h):
    # 创建一个矩形对象，参数为左上角坐标和宽高
    rect = pygame.Rect(x, y, w, h)
    # 在屏幕上绘制一个矩形，参数为屏幕对象、颜色和矩形对象
    pygame.draw.rect(screen, (50, 200, 50), rect)
    # 创建一个文字对象，参数为文字、是否抗锯齿、颜色
    text_surf = font.render(text, True, (255, 255, 255))
    # 在屏幕上绘制文字，参数为屏幕对象、文字对象和左上角坐标
    screen.blit(text_surf, (x + 10, y + 10))
    # 返回矩形对象
    return rect


# 定义一个函数，用于选择阵营
def faction_selection():
    # 创建一个空列表，用于存储按钮
    buttons = []
    # 设置按钮的y坐标
    y = 200
    # 遍历所有阵营
    for faction in FACTIONS:
        # 绘制按钮，并返回按钮和对应的阵营
        btn = draw_button(faction, 300, y, 200, 40)
        # 将按钮和对应的阵营添加到列表中
        buttons.append((btn, faction))
        # 更新按钮的y坐标
        y += 60
    # 返回按钮列表
    return buttons

# 定义一个绘制爆炸的函数，参数为x和y坐标
def draw_explosion(x, y):
    # 使用pygame绘制一个红色的圆，圆心为(x,y)，半径为10
    pygame.draw.circle(screen, (255,0,0), (x,y), 10)


def game_loop(faction):
    # 创建基地对象
    base = Base(faction)
    running = True

    while running:
        # 填充屏幕颜色
        screen.fill((30, 30, 30))

        # 绘制状态信息
        status = [
            f"防御值: {base.defense}",
            f"资源量: {base.resources}",
            f"科技点: {base.tech}",
            f"丧尸波次: {base.wave}/50"
        ]

        y = 50
        for line in status:
            # 渲染文本
            text = font.render(line, True, (255, 255, 255))
            # 绘制文本
            screen.blit(text, (50, y))
            y += 40

        # 操作按钮（参考‌:ml-citation{ref="8" data="citationList"}的UI设计）
        build_btn = draw_button("建造防御 (100)", 500, 400, 200, 40)
        research_btn = draw_button("研发科技 (50)", 500, 450, 200, 40)

        # 事件处理（参考‌:ml-citation{ref="5" data="citationList"}的事件循环）
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False

            if event.type == MOUSEBUTTONDOWN:
                # 建造防御按钮
                if build_btn.collidepoint(event.pos):
                    if base.resources >= 100:
                        base.resources -= 100
                        bonus = 35 if base.faction == "军事要塞" else 25
                        base.defense += bonus

                # 研发科技按钮
                elif research_btn.collidepoint(event.pos):
                    if base.tech >= 50:
                        base.tech -= 50
                        base.defense += 15

        # 丧尸攻击（参考‌:ml-citation{ref="1" data="citationList"}的攻击逻辑）
        if random.random() < 0.2:
            damage = 20 + random.randint(0, base.wave * 5)
            if base.faction == "军事要塞":
                damage = int(damage * 0.7)
            base.defense -= damage
            base.wave += 1

        # 刷新屏幕
        pygame.display.flip()
        # 控制帧率
        clock.tick(30)


# 主程序（参考‌:ml-citation{ref="3" data="citationList"}的主循环结构）
selection = faction_selection()
selected = None
while not selected:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        if event.type == MOUSEBUTTONDOWN:
            for btn, faction in selection:
                if btn.collidepoint(event.pos):
                    selected = faction

    pygame.display.flip()
    clock.tick(30)

game_loop(selected)
